package top.carrotguo.weibo.ui.person;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnMultiPurposeListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.adapter.PersonPagerAdapter;
import top.carrotguo.weibo.bean.UserInfo;
import top.carrotguo.weibo.contract.PersonDetailContract;
import top.carrotguo.weibo.presenter.PersonDetailPresenter;
import top.carrotguo.weibo.ui.base.BaseActivity;
import top.carrotguo.weibo.ui.base.BaseFragment;
import top.carrotguo.weibo.ui.base.MVPBaseActivity;
import top.carrotguo.weibo.utils.UIUtils;

public class PersonDetailActivity extends MVPBaseActivity<PersonDetailContract.View,PersonDetailPresenter>
    implements  PersonDetailContract.View{

    @BindView(R.id.iv_image_bg)
    ImageView ivBg;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_focus)
    TextView tvFocus;
    @BindView(R.id.tv_follows)
    TextView tvFollows;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_calendar)
    ImageView ivCalendar;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.iv_title_head)
    ImageView ivTitleHead;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tab_pager)
    TabLayout tabPager;
    @BindView(R.id.vp_container)
    ViewPager vpContainer;
    @BindView(R.id.head)
    ClassicsHeader head;
    @BindView(R.id.colapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbarLayout;

    private ArrayList<String> titles = new ArrayList<>();
    private PersonPagerAdapter mAdapter;
    //标题栏是否为白色背景
    private boolean isWhite = false;
    private PersonHomeFragment fragmentHome;
    private PersonHomeFragment fragment2;
    private PersonHomeFragment fragment3;
    public static final String USER = "USER";
    private UserInfo user;

    @Override
    public PersonDetailPresenter createPresenter() {
        return new PersonDetailPresenter();
    }

    @Override
    public int provideContentViewId() {
        return R.layout.fragment_person_detail;
    }

    @Override
    public Bundle getBundleExtras(Bundle bundle) {
        user = (UserInfo) bundle.getSerializable(USER);
        fragmentHome = PersonHomeFragment.getInstance(user.getUid());
        fragment2 = PersonHomeFragment.getInstance(user.getUid());
        fragment3 = PersonHomeFragment.getInstance(user.getUid());
        return null;
    }

    @Override
    protected void initView() {
        super.initView();
        initStatusBar();
        initViewPager();
        initRefreshing();
        initActionBar();
        initData();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        if (user != null) {
            Glide.with(mContext)
                    .load(user.getAvatar_large())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(ivHead);
            Glide.with(mContext)
                    .load(user.getAvatar_large())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(ivTitleHead);
            tvName.setText(user.getName());
            tvTitle.setText(user.getName());
            tvFocus.setText("关注："+user.getFavourites_count());
            tvFocus.setText("粉丝："+user.getFollowers_count());
        }
    }

    /**
     * 状态栏显示的控制
     */
    private void initActionBar() {
        collapsingToolbarLayout.setTitle("  ");
        appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
                if (-offset >= UIUtils.dip2px(200) - mToolbar.getHeight()) {
                    //显示标题栏
                    if (!isWhite) {
                        isWhite = true;
                        ivBack.setBackgroundResource(R.drawable.back_black);
                        ivCalendar.setBackgroundResource(R.drawable.calendar_black);
                        mToolbar.setBackgroundColor(UIUtils.getColor(R.color.white));
                        rlTitle.setVisibility(View.VISIBLE);
                        tvTitle.setText("Carrot");
                    }
                } else {
                    if (isWhite) {
                        isWhite = false;
                        ivBack.setBackgroundResource(R.drawable.arrow_back);
                        ivCalendar.setBackgroundResource(R.drawable.calendar);
                        rlTitle.setVisibility(View.INVISIBLE);
                        mToolbar.setBackgroundColor(UIUtils.getColor(R.color.transparent));
                    }
                }
            }
        });
    }

    private void initStatusBar() {
        immersionBar.titleBar(mToolbar).transparentStatusBar().init();
    }

    private void initViewPager(){
        //设置刷新字体颜色
        head.setAccentColor(UIUtils.getColor(R.color.white));
        titles.add("主页");
        titles.add("微博");
        titles.add("相册");
        for (int i=0; i<titles.size(); i++) {
            if (tabPager == null) {
                com.orhanobut.logger.Logger.e("Null"+i);
            }
            tabPager.addTab(tabPager.newTab().setText(titles.get(i)));
        }

        ArrayList<BaseFragment> fragments = new ArrayList<>();
        fragments.add(fragmentHome);
        fragments.add(fragment2);
        fragments.add(fragment3);
        mAdapter = new PersonPagerAdapter(getSupportFragmentManager(),fragments,titles);
        vpContainer.setAdapter(mAdapter);
        tabPager.setupWithViewPager(vpContainer);
    }

    /**
     * 下拉事件
     */
    public void initRefreshing(){
        refreshLayout.setOnMultiPurposeListener(new OnMultiPurposeListener() {
            @Override
            public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
            }

            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
            }

            @Override
            public void onHeaderPulling(RefreshHeader header, float percent, int offset, int headerHeight, int extendHeight) {
                int imgOffset = offset/2;
                ivBg.setTranslationY(imgOffset);
            }

            @Override
            public void onHeaderReleasing(RefreshHeader header, float percent, int offset, int headerHeight, int extendHeight) {
                int imgOffset = offset/2;
                ivBg.setTranslationY(imgOffset);
            }

            @Override
            public void onHeaderStartAnimator(RefreshHeader header, int headerHeight, int extendHeight) {
            }

            @Override
            public void onHeaderFinish(RefreshHeader header, boolean success) {
            }

            @Override
            public void onFooterPulling(RefreshFooter footer, float percent, int offset, int footerHeight, int extendHeight) {
            }

            @Override
            public void onFooterReleasing(RefreshFooter footer, float percent, int offset, int footerHeight, int extendHeight) {
            }

            @Override
            public void onFooterStartAnimator(RefreshFooter footer, int footerHeight, int extendHeight) {
            }

            @Override
            public void onFooterFinish(RefreshFooter footer, boolean success) {
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void back(View view){
        onBackPressed();
    }

}
